package com.cango.student.tcp;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.time.LocalTime;

/**
 * @program: student
 * @description:
 * @author: xie jiefeng
 * @create: 2019-06-17 15:26
 */
public class TcpSendAndRecv {
    public static void main(String[] args) throws IOException {
        // 利用socket模拟了一个简单客户端，只进行连接，读取，打印
        for (int i = 0; i < 8000; i++) {
            new Thread(new tcp(i)).start();
        }
    }

    public static class tcp implements Runnable {
        int i;

        public tcp(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            String value = "";
            try {

                try (Socket client = new Socket("172.16.17.25", 8080)) {
                    LocalTime now = LocalTime.now();
                    value = i + " start " + now;
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(client.getInputStream())));
                    bufferedReader.lines().forEach(s -> System.out.println(s));
                } finally {
                    LocalTime now = LocalTime.now();
                    value = value + "    " + i + " end " + now;

                    System.out.println(value);
                }
            } catch (Exception ex) {
                LocalTime now = LocalTime.now();
                value = value + "    " + i + " error " + now;

                System.out.println(value);
            } finally {
            }

        }
    }
}
